<?php
class Usermodel extends CI_Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    function insertUser($userArray){
		$sql = "INSERT INTO eq_user (username, password, email, title, first_name, last_name, partner_id, is_wife, user_type) VALUES(?,?,?,?,?,?,?,?,?)";
		if($this->db->query($sql, $userArray)){
			return $this->db->insert_id();
		}else{
			return false;
		}
	}
	function updateUser($userArray){
		$sql = "UPDATE eq_user SET password= ?, email = ?, title = ?, first_name = ?, last_name = ?, partner_id = ?, is_wife = ?, user_type = ? WHERE id = ? ";
		if($this->db->query($sql, $userArray)){
			return true;
		}else{
			return false;
		}
	}
	function updateAdminUser($userArray){
		$sql = "UPDATE eq_user SET username = ?, password= ?, email = ?, title = ?, first_name = ?, last_name = ?, partner_id = ?, is_wife = ?, user_type = ?, dr_note = ? WHERE id = ? ";
		if($this->db->query($sql, $userArray)){
			return true;
		}else{
			return false;
		}
	}
	function updatePassByEmail($email, $password){
		$sql = "UPDATE eq_user SET password = ? WHERE email = ? ";
		if($this->db->query($sql, array($password ,$email))){
			return true;
		}else{
			return false;
		}
	}
	function getAdminInfo(){
		$query = $this->db->get_where('eq_user', array('user_type' => 0));
		$adminInfor = $query->result();
		if(count($adminInfor) > 0){
			return $adminInfor[0];
		}else{
			return null;
		}
	}
	function validateUser($username, $password){
		$query = $this->db->get_where('eq_user', array('username' => $username, 'password' => md5($password)));
		$userInfor = $query->result();
		if(count($userInfor) > 0){
			return $userInfor[0];
		}else{
			return null;
		}
	}
	function checkUsernameExisted($username){
		$query = $this->db->get_where('eq_user', array('username' => $username));
		$userInfor = $query->result();
		if(count($userInfor) > 0){
			return $userInfor[0];
		}else{
			return null;
		}
	}
	function checkEmailExisted($email){
		$query = $this->db->get_where('eq_user', array('email' => $email));
		$userInfor = $query->result();
		if(count($userInfor) > 0){
			return $userInfor[0];
		}else{
			return null;
		}
	}
	
	function getUserById($id){
		$query = $this->db->get_where('eq_user', array('id' => $id));
		$userInfor = $query->result();
		if(count($userInfor) > 0){
			return $userInfor[0];
		}else{
			return null;
		}
	}
	function getUserByUsername($username){
		$query = $this->db->get_where('eq_user', array('username' => $username));
		$userInfor = $query->result();
		if(count($userInfor) > 0){
			return $userInfor[0];
		}else{
			return null;
		}
	}
    function getAllClient()
    {
        $query = $this->db->get_where('eq_user', array('user_type' => 1));
        return $query->result();
    }
}